﻿using System;
using System.Text;
using System.Threading;
using Virvar.Net;

namespace TestClient
{
    class Program
    {
        static Encoding enc = Encoding.Default;
        static IMessager messager;

        static void Main(string[] args)
        {
            messager = new UdpMessager("127.0.0.1", 7778, 7777);
            Thread thread = new Thread(new ThreadStart(Receive));
            thread.Start();
            Console.WriteLine("Это клиент. Пиши!");
            while (true)
            {
                string text = Console.ReadLine();
                if (thread.IsAlive)
                {
                    messager.Send(enc.GetBytes(text));
                }
                else
                {
                    break;
                }
            }
            Console.WriteLine("Сервер не работает. Жми для выхода!");
            Console.ReadKey();
        }

        static void Receive()
        {
            while (true)
            {
                byte[] bytes = messager.Receive();
                if (bytes == null)
                    break;
                string text = "received: " + enc.GetString(bytes);
                Console.WriteLine(text);
            }
            messager.Close();            
        }
    }
}
